package tq.com.tqteacher.model;

import org.json.JSONArray;
import org.json.JSONObject;

public class TQCommentOverInfoModel {
    private int count;

    private TQCommentInfoModel mCommentInfoModel;
    public TQCommentOverInfoModel(){}
    public TQCommentOverInfoModel(JSONObject pObject){
        refresh(pObject);
    }

    public void refresh(JSONObject pObject){
        if(pObject == null)return;

        count = pObject.optInt("total_comment");
        JSONArray all = pObject.optJSONArray("latest_comment");
        if(all == null || all.length() <= 0) return;

        mCommentInfoModel = new TQCommentInfoModel(all.optJSONObject(0));
    }

    public int getCount() {
        return count;
    }

    public TQCommentInfoModel getCommentInfoModel() {
        return mCommentInfoModel;
    }

    static public class TQCommentInfoModel{
        private TQUserInfo mUserInfo = new TQUserInfo();
        private String content;
        private String date;
        private int type;

        private int id;

        private TQUserInfo to ;
        private int toGroup;

        public TQCommentInfoModel(JSONObject pObject){
            mUserInfo.copy(TQLogin.getCurrent().getUserInfo());
            refresh(pObject);
        }

        public TQCommentInfoModel(){
            mUserInfo.copy(TQLogin.getCurrent().getUserInfo());
        };

        public void refresh(JSONObject pObject){
            if(pObject == null) return;

            JSONObject user = pObject.optJSONObject("creator");
            if(user != null){
                mUserInfo.refresh(user);
            }

            if(mUserInfo.getId() == 0){
                mUserInfo.copy(TQLogin.getCurrent().getUserInfo());
            }

            content = pObject.optString("content");
            date = pObject.optString("time");
            type = pObject.optInt("content_type");
            id = pObject.optInt("id");
        }
        public void refreshMessage(JSONObject pObject){
            if(pObject == null) return;

            JSONObject user = pObject.optJSONObject("sender");
            if(user != null){
                mUserInfo.refresh(user);
            }
            JSONObject to = pObject.optJSONObject("receiver");
            if(to != null){
                this.to = new TQUserInfo();
                this.to.refresh(to);
            }
            this.toGroup = pObject.optInt("to_gid");

            if(mUserInfo.getId() == 0){
                mUserInfo.copy(TQLogin.getCurrent().getUserInfo());
            }

            content = pObject.optString("content");
            date = pObject.optString("time");
            type = pObject.optInt("content_type");
            id = pObject.optInt("id");
        }

        //im message
        public void refreshFromIm(JSONObject pObject){
            if(pObject == null) return;

            String sname = pObject.optString("sname");
            int sId = pObject.optInt("suid");
            int riD = pObject.optInt("ruid");
            String pic = pObject.optString("spic");

            content = pObject.optString("c");
            type = pObject.optInt("ctype");
            mUserInfo.setPic(pic);
            mUserInfo.setName(sname);
            mUserInfo.setId(sId);

            this.to = new TQUserInfo();
            this.to.setId(riD);
            toGroup = pObject.optInt("rgid");
        }

        public TQUserInfo getTo() {
            return to;
        }

        public int getToGroup() {
            return toGroup;
        }

        @Override
        public boolean equals( Object obj) {
            if(obj instanceof TQCommentInfoModel){
                return ((TQCommentInfoModel)obj).getId() == this.getId();
            }
            return super.equals(obj);
        }

        public TQUserInfo getUserInfo() {
            return mUserInfo;
        }

        public String getContent() {
            return content;
        }

        public String getDate() {
            return date;
        }
        public int getType(){return type;}
        public int getId(){return id;}

        public void setContent(String pContent) {
            content = pContent;
        }

        public void setDate(String pDate) {
            date = pDate;
        }

        public void setType(int pType) {
            type = pType;
        }

    }
}
